//
//  UIImage+YXVideo.m
//  CreditCard
//
//  Created by 杨旭 on 2018/1/26.
//  Copyright © 2018年 杨旭. All rights reserved.
//

#import <AVFoundation/AVFoundation.h>
#import "UIImage+YXVideo.h"

@implementation UIImage (YXVideo)
+(UIImage *)imageWithVideo:(NSURL *)videoURL {
    // 根据视频的URL创建AVURLAsset
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    // 根据AVURLAsset创建AVAssetImageGenerator对象
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    // 定义获取0帧处的视频截图
    CMTime time = CMTimeMake(27, 10);
    NSError *error = nil;
    CMTime actualTime;
    // 获取time处的视频截图
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    // 将CGImageRef转换为UIImage
    UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return thumb;
    
}

@end
